package cn.demoxy.controller;

import cn.demoxy.annotation.Log;
import cn.demoxy.component.MinioUtils;
import cn.demoxy.enums.BusinessTypeEnum;
import cn.demoxy.model.dto.DemoUserDTO;
import cn.demoxy.model.entity.DemoUser;
import cn.demoxy.result.R;
import cn.demoxy.service.IDemoFileService;
import cn.demoxy.service.IDemoUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * @auther demo
 * @date 2022/5/17 21:37
 * 用户表控制层
 */
@Api(tags = "用户接口信息")
@RestController
@RequestMapping("/admin/demo-user")
@AllArgsConstructor
@CrossOrigin
public class DemoUserController {

    /**
     * 用户service
     */
    IDemoUserService userService;

    private MinioUtils minioUtils;


    /**
     * 根据条件分页查询用户列表
     *
     * @param userDTO
     * @return
     */
    @ApiOperation(value = "根据条件分页查询用户列表")
    @PostMapping("/page")
    @RequiresPermissions("demo-user:page")
    public R page(@RequestBody DemoUserDTO userDTO) {
        return R.ok(userService.findAllUserByKeyword(userDTO));
    }

    /**
     * 添加用户信息
     *
     * @param user
     * @return
     */
    @ApiOperation(value = "添加用户信息")
    @PostMapping("/save")
    @Log(title = "用户管理用户新增",businessType = BusinessTypeEnum.INSERT)
    @RequiresPermissions("demo-user:save")
    public R save(@RequestBody DemoUser user) {
        return R.ok(userService.saveUser(user));
    }

    /**
     * 编辑用户信息
     *
     * @param user
     * @return
     */
    @ApiOperation(value = "编辑用户信息")
    @PostMapping("/update")
    @Log(title = "用户管理用户信息修改",businessType = BusinessTypeEnum.UPDATE)
    @RequiresPermissions("demo-user:update")
    public R update(@RequestBody DemoUser user) {
        return R.ok(userService.update(user));
    }

    /**
     * 删除用户信息
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "删除用户信息")
    @PostMapping("/delete/{id}")
    @Log(title = "用户管理用户删除",businessType = BusinessTypeEnum.DELETE)
    @RequiresPermissions("demo-user:delete")
    public R delete(@PathVariable("id") String id) {
        return R.ok(userService.removeById(id));
    }

    /**
     * 用户信息导出
     *
     * @param response
     */
    @ApiOperation(value = "用户信息excel导出")
    @GetMapping(value = "/exportData")
    @Log(title = "用户管理excel导出",businessType = BusinessTypeEnum.EXPORT)
    @RequiresPermissions("demo-user:exportData")
    public void exportData(HttpServletResponse response) {
        userService.exportData(response);
    }


    /**
     * 用户信息excel导入
     *
     * @param file
     * @return
     */
    @ApiOperation(value = "用户信息excel导入")
    @PostMapping("/importData")
    @Log(title = "用户管理excel导入",businessType = BusinessTypeEnum.IMPORT)
    @RequiresPermissions("demo-user:importData")
    public R importData(MultipartFile file) {
        userService.importUserData(file);
        return R.ok();
    }

    /**
     * 根据条件分页查询用户列表
     *
     * @param userDTO
     * @return
     */
    @ApiOperation(value = "根据条件分页查询在线用户列表")
    @PostMapping("/onlinePage")
//    @RequiresPermissions("demo-user:onlinePage")
    public R onlinePage(@RequestBody DemoUserDTO userDTO) {
        return R.ok(userService.findAllOnlineUser(userDTO));
    }

    /**
     * 根据用户名强制退出用户
     *
     * @param username
     * @return
     */
    @ApiOperation(value = "根据用户名强制退出用户")
    @GetMapping("/forcedReturnById")
    @Log(title = "在线用户强制退出",businessType = BusinessTypeEnum.FORCED_RETURN)
//    @RequiresPermissions("demo-user:forcedReturnById")
    public R forcedReturnById(String username) {
        return R.ok(userService.forcedReturnById(username));
    }

    /**
     * 通过id查询
     * @params id
     * @return
     */
    @ApiOperation(value = "通过id查询")
    @GetMapping("/get")
    @RequiresPermissions("demo-user:get")
    public R get(String id) {
        return R.ok(this.userService.getById(id));
    }


}
